package com.company;
import java.io.IOException;
import java.lang.reflect.Method;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

public class workClassLoader extends ClassLoader{
    public static void main(String[] args) throws Exception  {
        // write your code here
        ClassLoader classLoader = new workClassLoader();
        // 加载相应的类
        Class<?> clazz = classLoader.loadClass("Hello");
        // 创建对象
        Object instance = clazz.getDeclaredConstructor().newInstance();
        // 调用实例方法
        Method method = clazz.getMethod("hello");
        method.invoke(instance);
    }

    @Override
    protected Class<?> findClass(String name) throws ClassNotFoundException {
        Path path = Paths.get(System.getProperty("user.dir")+"\\src\\com\\company\\Hello.xlass");
        byte[] helloBase64 = new byte[0];
        try {
            helloBase64 = Files.readAllBytes(path);
        } catch (IOException e) {
            e.printStackTrace();
        }
        for (int i =0; i<helloBase64.length;i++){
            helloBase64[i] = (byte) (255-helloBase64[i]);
        }
        return defineClass(name, helloBase64, 0, helloBase64.length);
    }
}
